<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        :data="depts"
        :props="defaultProps"
        :expand-on-click-node="false"
      >
        <template v-slot="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <!-- @command是组件提供的 -->
              <el-dropdown @command="hCommand($event,data.id)">

                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 添加子部门 -->
    <add-dept ref="dept" :show-dialog.sync="showDialog" :cur-id="curId" :is-edit="isEdit" @updateDepartment="getDepartment" />
    <!-- .sync是语法糖 双向绑定 接收传值  监听update事件 -->
    <!-- 父传子 点击的id传过去 -->
  </div>
</template>

<script>
import { delDepartment, getDepartment } from '@/api/department'
import { arr2tree } from '@/utils'
import addDept from './components/add_dept.vue'
export default {
  name: 'Department',
  components: { addDept },
  data() {
    return {
      curId: '', // 存储当前点击的id,
      isEdit: false, // 判断是编辑还是添加
      depts: [{
        name: '传智教育',
        children: [
          { name: '总裁办' },
          { name: '行政部' },
          { name: '财务部' }
        ]
      }],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false// 控制 添加部门的显示和隐藏
    }
  },
  created() {
    this.getDepartment()// 部门列表
  },
  methods: {
    async hCommand(type, id) {
      this.curId = id
      if (type === 'add') {
        this.isEdit = false
        this.showDialog = true
      } else if (type === 'edit') {
        this.isEdit = true
        // 显示弹框
        this.showDialog = true
        // 更新props- 异步动作 数据回显
        // 父组件调用子组件的方法从子组件中来获取数据
        this.$nextTick(() => {
          this.$refs.dept.getDetail()
        })
        // 这里为什么要用$nextTick，因为弹出层之后紧接着要调用子组件方法，第一次弹出层时，里面的组件还未完成渲染，所以要等到showDialog这个属性设置的数据完成渲染之后才进行调用
      } else if (type === 'del') {
        this.$confirm('你确定要删除吗?').then(() => {
          // 用户点击了确认
          this.delete(id)
        }).catch(() => {})
      }
    },

    // 获取-部门列表
    async getDepartment() {
      const res = await getDepartment()
      // console.log(res.data)
      this.depts = arr2tree(res.data, 1)
    },
    // 删除
    async delete(id) {
      await delDepartment(id)
      this.$message.success('删除成功')
      // 更新数据
      this.getDepartment()
    }

  }

}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
